package com.example.book_system.controller;

import com.example.book_system.constant.Constants;
import com.example.book_system.model.UserInfo;
import com.example.book_system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public boolean login(String name, String password, HttpSession session){
        if(!StringUtils.hasLength(name) || !StringUtils.hasLength(password)){
            return false;
        }

        UserInfo userInfo = userService.queryUserByName(name);
        if(userInfo == null || !password.equals(userInfo.getPassword())){
            return false;
        }

        session.setAttribute(Constants.SESSION_USER_KEY, userInfo);

        return true;
    }
}
